<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com web:http://www.jameson512.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 21:48:08
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
// 增加广告联盟代码
$topicfield = C::t('#jameson_read#jamesonread_topics')->fetch_all_field();
if (!$topicfield['adv']) {
    runquery("alter table cdb_jamesonread_topics add `adv` text");
}
if($topicfield['image']['Type'] != "text"){
    runquery("alter table cdb_jamesonread_topics modify column image text;");
}
$bookmarkfield = C::t('#jameson_read#jamesonread_bookmarks')->fetch_all_field();
if (!$bookmarkfield['hash']) {
    runquery("alter table cdb_jamesonread_bookmarks add `hash` varchar(255) NOT NULL DEFAULT '0';");
}

// 打赏和佣金 分享 书签
$sqlcreate = <<<EOF
CREATE TABLE IF NOT EXISTS  `cdb_jamesonread_update` (
  `book_id` int(11) unsigned  NOT NULL,
  `tid` int(11) unsigned  NOT NULL,
  `pid` int(11) unsigned  NOT NULL,
  `category_id` int(11) unsigned  NOT NULL,
  `parent_id` int(11) unsigned  NOT NULL,
  `addtime` int(11) unsigned  NOT NULL,
  `book_name` char(255) NOT NULL,
  `image` char(255) NOT NULL,
  `subject` char(255) NOT NULL,
  `author` char(255) NOT NULL,
  KEY `category_id` (`category_id`),
  KEY `parent_id` (`parent_id`),
  PRIMARY KEY `book_id` (`book_id`)
);
EOF;
runquery($sqlcreate);
if (DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 120)) < 4) {
    $sqldibunav = <<<EOF
		INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujidibudaohang"]}', '{$installlang["shoujidibudaohang"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 120, '', '', '');
		INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujidibudaohang"]}', '{$installlang["shoujidibudaohang"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 120, '', '', '');
		INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujidibudaohang"]}', '{$installlang["shoujidibudaohang"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 120, '', '', '');
		INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujidibudaohang"]}', '{$installlang["shoujidibudaohang"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 120, '', '', '');
EOF;
    runquery($sqldibunav);
}
if (DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 5)) < 21) {
    // 增加的头条
    $sqltopics = <<<EOF
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["toutiao"]}18', '', '0', '0', '0', '0', '0', '0', '1', '0', '17', '5', '{$installlang["toutiaobiaoti"]}18', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["toutiao"]}19', '', '0', '0', '0', '0', '0', '0', '1', '0', '17', '5', '{$installlang["toutiaobiaoti"]}19', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["toutiao"]}20', '', '0', '0', '0', '0', '0', '0', '1', '0', '17', '5', '{$installlang["toutiaobiaoti"]}20', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["toutiao"]}21', '', '0', '0', '0', '0', '0', '0', '1', '0', '17', '5', '{$installlang["toutiaobiaoti"]}21', '#','');
EOF;
    runquery($sqltopics);
}
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 6))) {
    // 手机幻灯
    $sqlshoujihuandeng = <<<EOF
INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujihuandeng"]}1', '{$installlang["kuangaobi"]}', '0', '0', '0', '0', '0', '0', '1', '0', '30', '6', './source/plugin/jameson_read/images/hh1.jpg', '#','');
INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujihuandeng"]}2', '{$installlang["kuangaobi"]}', '0', '0', '0', '0', '0', '0', '1', '0', '31', '6', './source/plugin/jameson_read/images/hh2.jpg', '#','');
INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujihuandeng"]}3', '{$installlang["kuangaobi"]}', '0', '0', '0', '0', '0', '0', '1', '0', '32', '6', './source/plugin/jameson_read/images/hh3.jpg', '#','');
INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujihuandeng"]}4', '{$installlang["kuangaobi"]}', '0', '0', '0', '0', '0', '0', '1', '0', '33', '6', '', '#','');
EOF;
    runquery($sqlshoujihuandeng);
}
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 101))) {
    $piaosql = <<<EOF
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["yuepiao"]}', '{$installlang["piaojutishi"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 101, '1', '10', '1');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["tuijianpiao"]}', '{$installlang["piaojutishi"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 102, '2', '20', '2');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["cuigengpiao"]}', '{$installlang["piaojutishi"]}', 0, 0, 0, 0, 0, 0, 1, 0, 1, 103, '3', '30', '3');
EOF;
    runquery($piaosql);
}
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 7))) {
    // 阅读页广告
    // pc和手机阅读页广告
    $sqlyuedugg = <<<EOF
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["pcyuedu"]}1',     '{$installlang["pcyuedugg"]}',     '0', '0', '0', '0', '0', '0', '1', '0', '30', '7', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["pcyuedu"]}2',     '{$installlang["pcyuedugg"]}',     '0', '0', '0', '0', '0', '0', '1', '0', '31', '7', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujiyuedu"]}1', '{$installlang["shoujiyuedugg"]}', '0', '0', '0', '0', '0', '0', '1', '0', '32', '8', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujiyuedu"]}2', '{$installlang["shoujiyuedugg"]}', '0', '0', '0', '0', '0', '0', '1', '0', '33', '8', '', '#','');
EOF;
    runquery($sqlyuedugg);
}
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 44))) {
    $appsql = <<<EOF
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (44,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (44,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (44,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (55,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (55,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (55,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (55,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (66,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (66,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (66,'','');
		INSERT INTO `cdb_jamesonread_topics` (type,image,url)  VALUES (66,'','');
EOF;
    runquery($appsql);
}

if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 88))) {
    runquery("INSERT INTO `cdb_jamesonread_topics` (type,adv)  VALUES (88,'');");
}
// 手机发现 书城广告
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 11))) {
    // 手机导航
    $faxianadv = <<<EOF
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}1',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}2',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}3',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}4',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}5',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}6',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}7',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}8',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '11', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}9',   'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}10',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}11',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}12',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}13',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}14',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}15',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["faxianadv"]}16',  'width:100%', '0', '0', '0', '0', '0', '0', '1', '0', '33', '12', '', '#','');
	INSERT INTO `cdb_jamesonread_topics` VALUES (null, '{$installlang["shoujibanhaoshutuijian"]}', '', '0', '0', '0', '0', '0', '0', '1', '0', '33', '13', '', '#','');
EOF;
    runquery($faxianadv);
}
// 增加作者福利
if (!DB::result_first('SELECT count(*) FROM %t WHERE type=%d', array('jamesonread_topics', 38))) {
    runquery("INSERT INTO `cdb_jamesonread_topics` VALUES (null, 'author_gift', '', '0', '0', '0', '0', '0', '0', '1', '0', '33', '38', '', '#','');");
}
// 增加是否允许编辑章节字段
$authorfield = C::t('#jameson_read#jamesonread_authors')->fetch_all_field();
if (!$authorfield['allowedit']) {
    runquery("alter table cdb_jamesonread_authors add `allowedit` int(2) default '1'");
}
if (!$authorfield['groupid']) {
    runquery("alter table cdb_jamesonread_authors add `groupid` int(11) default '1'");
}
if (!$authorfield['yuepiao']) {
    runquery("
    	alter table cdb_jamesonread_authors add `yuepiao` int(11) default '0';
    	alter table cdb_jamesonread_authors add `tuijianpiao` int(11) default '0';
    	alter table cdb_jamesonread_authors add `cuigengpiao` int(11) default '0';
    ");
}
if (!$authorfield['zgsj']) {
    runquery("
    	alter table cdb_jamesonread_authors add `zgsj` int(11) default '0';
    	alter table cdb_jamesonread_authors add `qzsj` int(11) default '0';
    	alter table cdb_jamesonread_authors add `setsj` varchar(255) default 'zgsj';
    ");
}
// 增加字数字段
$columfield = C::t('#jameson_read#jamesonread_colums')->fetch_all_field();
if (!$columfield['length']) {
    runquery("alter table cdb_jamesonread_colums add `length` int(11) unsigned default '5000'");
}
if(!preg_match("/bigint/i",$columfield['zhangjie']['Type'])){
	runquery("alter table cdb_jamesonread_colums modify column zhangjie bigint(18) unsigned default '1';");
}
if (!$columfield['caijiid']) {
    runquery("alter table cdb_jamesonread_colums add `caijiid` varchar(255) DEFAULT NULL");
}
// 增加图书 属性字段
$bookfield = C::t('#jameson_read#jamesonread_books')->fetch_all_field();
if (!$bookfield['shuxing']) {
    runquery("alter table cdb_jamesonread_books add `shuxing` char(30)");
}
if (!$bookfield['authorinfo']) {
    runquery("alter table cdb_jamesonread_books add `authorinfo` text");
}
// 增加图书  字数字段
if (!$bookfield['zishu']) {
    runquery("alter table cdb_jamesonread_books add `zishu` mediumint(20) unsigned DEFAULT '0'");
}
if (!$bookfield['caijiid']) {
    runquery("alter table cdb_jamesonread_books add `caijiid` varchar(255) DEFAULT NULL");
}
// 增加图书  是否是pdf  is_pdf
if (!$bookfield['is_pdf']) {
    runquery("alter table cdb_jamesonread_books add `is_pdf` tinyint(1) NOT NULL DEFAULT '0'");
}
if (!$bookfield['hascol']) {
    runquery("alter table cdb_jamesonread_books add `hascol` int(11) NOT NULL DEFAULT '0'");
}
if (!$bookfield['parent_id']) {
    runquery("alter table cdb_jamesonread_books add `parent_id` int(11) NOT NULL DEFAULT '0'");
}
if (!$bookfield['qzfid']) {
    runquery("alter table cdb_jamesonread_books add `qzfid` int(11) NOT NULL DEFAULT '0'");
}
if (!$bookfield['sign']) {
    runquery("alter table cdb_jamesonread_books add `sign` int(2) NOT NULL DEFAULT '0'");
}
if (!$bookfield['yuepiao']) {
    runquery("
    	alter table cdb_jamesonread_books add `yuepiao`  int(11)  DEFAULT '0';
    	alter table cdb_jamesonread_books add `tuijianpiao` int(11)  DEFAULT '0';
    	alter table cdb_jamesonread_books add `cuigengpiao` int(11)  DEFAULT '0';
    	");
}

$searchbiao = C::t('#jameson_read#jamesonread_search')->fetch_all_field();
if (!$searchbiao['tag']) {
    runquery("alter table `cdb_jamesonread_search` add `tag` varchar(255) DEFAULT NULL;");
    runquery("alter table `cdb_jamesonread_search` add `tagcounts` int(11) DEFAULT NULL;");
}
// 如果不存在skey=jameson_read
if (!DB::result_first('SELECT count(*) FROM %t WHERE skey=%s', array('common_setting', 'jameson_read'))) {
    runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('jameson_read','163')");
} else {
    $tmpname = DB::result_first('SELECT svalue FROM %t WHERE skey=%s LIMIT %d', array('common_setting', 'jameson_read', 1));
    if (!$tmpname || ($tmpname == 'zidai') || ($tmpname == 'boot')) {
        runquery("UPDATE  `cdb_common_setting` SET `svalue`='163' WHERE `skey`='jameson_read'");
    }
}
// 如果不存在skey=jameson_read
if (!DB::result_first('SELECT count(*) FROM %t WHERE skey=%s', array('common_setting', 'xf512seo_index'))) {
	$pluginid = DB::result_first('SELECT pluginid FROM '.DB::table('common_plugin').' WHERE '.DB::field('identifier','jameson_read'));
	$seoindex = DB::result_first('SELECT value FROM '.DB::table('common_pluginvar').' WHERE '.DB::field('variable','jamesonread_title') .' AND '.DB::field('pluginid',$pluginid));
    runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512seo_index','".$seoindex."_[zhanming]')");
	runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512seo_book','[shuming]_[zifenlei]_[fufenlei]_[zhanming]')");
	runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512seo_read','[zhangjie]_[shuming]_[zifenlei]_[fufenlei]_[zhanming]')");
	runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512seo_cate','[cate]_[zhanming]')");
	runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512seo_shaixuan','[shaixuan]_[zhanming]')");
}
if(!DB::result_first('SELECT count(*) FROM %t WHERE skey=%s', array('common_setting', 'xf512read_mbstyle'))){
	runquery("INSERT INTO `cdb_common_setting` (`skey`,`svalue`) VALUES ('xf512read_mbstyle','display: block;margin: 8px auto;padding: 10px;text-align: center;clear: both;background: #F44336;font-size: 14px;color: #fff;')");
}
// 插入作者表
$books      = DB::fetch_all('SELECT book_id,hascol,zishu,uid,category_id FROM %t', array('jamesonread_books'));
$categorys  = DB::fetch_all('SELECT parent_id FROM %t WHERE parent_id>%d', array('jamesonread_categorys', 0), 'category_id');
$allauthors = DB::fetch_all('SELECT author_id FROM %t', array('jamesonread_authors'));
$alluids    = $newuid    = array();
foreach ($allauthors as $key => $value) {
    $alluids[$value['author_id']] = $value['author_id'];
}
foreach ($books as $key => $value) {
    if (!in_array($value['uid'], $alluids)) {
        $newuid[$value['uid']] = $value['uid'];
    }
}
foreach ($newuid as $key => $value) {
    runquery("INSERT INTO `cdb_jamesonread_authors` (`author_id`,`status`,`edit`,`allowedit`,`zgsj`,`qzsj`,`setsj`,`yuepiao`,`tuijianpiao`,`cuigengpiao`) VALUES ('" . $value . "',1,1,1,0,0,'zgsj',0,0,0)");
}

$finish = true;